<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Action;
use think\Session;
class Index extends Controller
{
	public $template='';//这里声明
	public $roleid;
	protected $beforeActionList = [
		'check' => ['except'=>'index'],
	];
	
	public function check()
	{
		$userdata=session("userdata");
		if(empty($userdata))
		{
			$this->error('非法登录');
		}
	}

	public function _initialize()
    {
		$userdata=session("userdata");
        $this->roleid=$userdata['role'];
		$this->template='admin';
		$setting=Db::name('settinginfo')->where('id',Config()['wid'])->find();//网站配置加载
		
		$menuinfo=Db::name('menuinfo')->where('role','<=',$this->roleid)->order('sort')->select();//后台管理左侧菜单加载
		$this->assign("menuinfo",$menuinfo);
		$this->assign('setting',$setting);	
        $this->template='admin';
    }
    public function index() 
    {
        return $this->fetch($this->template.'/login');
    }
	public function menu(){
		$info = array(
		'操作系统'=>PHP_OS,
		'运行环境'=>$_SERVER["SERVER_SOFTWARE"],
		'PHP运行方式'=>php_sapi_name(),
		'ThinkPHP版本'=>THINK_VERSION.' [ <a href="http://thinkphp.cn" target="_blank">查看最新版本</a> ]',
		'上传附件限制'=>ini_get('upload_max_filesize'),
		'执行时间限制'=>ini_get('max_execution_time').'秒',
		'服务器时间'=>date("Y年n月j日 H:i:s"),
		'北京时间'=>gmdate("Y年n月j日 H:i:s",time()+8*3600),
		'服务器域名/IP'=>$_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]',
		'剩余空间'=>round((disk_free_space(".")/(1024*1024)),2).'M',
		'管理系统版本'=>'Ver1.0',
		);
		
		$this->assign('info',$info);
		return $this->fetch($this->template.'/index');
	}
	public function datagrid($id)
	{
		
		$table=Db::name('menuinfo')->where('id',$id)->find();
		$this->assign('tablename',$table['table']);
		action('service/datagridfield',$table['table']);
		$this->assign('add',$table['add']);
		$this->assign('qxm',json_encode(json_decode($table['qxm'],true)[$this->roleid]));
		
		return $this->fetch($this->template.'/datagrid');
		
	}
	public function setting()
	{
		$name="网站配置";
		$table='settinginfo';
		$fields=action('service/rest','settinginfo');
		$data=Db::name('settinginfo')->where('id',config()['wid'])->find();
		foreach($fields as $key=>$val)
		{
			$fields[$key]['value']=$data[$val['column_name']];
		}
		$this->assign('table',$table);
		$this->assign('name',$name);
		$this->assign('fields',$fields);
		return $this->fetch($this->template.'/restructure');
		
	}
	public function addnews()
	{
		$name="添加新闻";
		$table="newsinfo";
		$fields=action('service/rest',$table);
		
		foreach($fields as $key=>$val)
		{
			if($val['input']=='select')
			{
				$fields[$key]['value']=Db::name('newscateinfo')->where('wid',config()['wid'])->select();
			}
			
		}
		$this->assign('table',$table);
		$this->assign('name',$name);
		$this->assign('fields',$fields);
		return $this->fetch($this->template.'/restructure');
		
	}
	public function adddeptnews()
	{
		$name="添加部门咨询";
		$table="deptnewsinfo";
		$fields=action('service/rest',$table);
		
		foreach($fields as $key=>$val)
		{
			if($val['input']=='select')
			{
				$fields[$key]['value']=Db::name('deptcateinfo')->select();
				
			}
			
		}
		$this->assign('table',$table);
		$this->assign('name',$name);
		$this->assign('fields',$fields);
		return $this->fetch($this->template.'/restructure');
		
	}
	public function restructure($id)
	{
		$name="添加信息";
		$table=Db::name('menuinfo')->where('id',$id)->find();
		$fields=action('service/rest',$table['table']);
		$this->assign('name',$name);
		$this->assign('table',$table['table']);
		$this->assign('fields',$fields);
		return $this->fetch($this->template.'/restructure');
		
	}
	public function temp()
	{
		return $this->fetch($this->template.'/template');
	}
	public function edit($id,$table)
	{
		$where='';
		if(isset($id))
			$where['id']=$id;
		if(isset($wid))
			$where['wid']=$wid;
		
		$name="编辑信息";
		$fields=action('service/rest',$table);
		
		
		$data=Db::name($table)->where($where)->find();

		foreach($fields as $key=>$val)
		{
			if($fields[$key]['input']!='select')
				$fields[$key]['value']=$data[$val['column_name']];
			else
				$fields[$key]['selectvalue']=$data[$val['column_name']];
				
			
		}
		foreach($fields as $key=>$val)
		{
			if($val['input']=='select')
			{
				if($table=='newsinfo'){
					$fields[$key]['value']=Db::name('newscateinfo')->where('wid',config()['wid'])->select();
					
				}
				else
				{
					$fields[$key]['value']=Db::name('deptcateinfo')->select();
					
				}
			}
			
		}
		$this->assign('name',$name);
		$this->assign('table',$table);
		$this->assign('fields',$fields);
		return $this->fetch($this->template.'/edit');
		
	}
	public function ck($id)
	{
		
		$data=Db::name('deptnewsinfo')->where('id',$id)->find();
		$this->assign('data',$data);
		return $this->fetch($this->template.'/ck');
	}
	public function quit()
	{
		session::delete('userdata');
		$this->success('退出成功',url('admin/login/index'));
	}
	public function rejson()
	{
		return ;
	}
	/*
	public function dirtemp()
	{
		$d = dir('public/template/admin/common');

		echo "Handle: " . $d->handle . "<br>";
		echo "Path: " . $d->path . "<br>";

		while (($file = $d->read()) !== false){
		  echo "filename: " . $file . "<br>";
		}
		$d->close();
	}
	public function rejson()
	{
		$data=Db::name('Deptcateinfo')->select();
		foreach($data as $k => $v)
		{
			$status[$v['id']]=$v['title'];
		}
		var_dump($status);
	}*/
}
